package cn.com.captureNetPacket.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.com.captureNetPacket.dao.ConfigInfoMapper;
import cn.com.captureNetPacket.model.ConfigInfo;
import cn.com.captureNetPacket.model.ParamCondition;
import cn.com.captureNetPacket.model.WebResult;
import cn.com.captureNetPacket.service.ConfigInfoService;

@Service
public class ConfigInfoServiceImpl implements ConfigInfoService {

	@Autowired
	private ConfigInfoMapper configInfoMapper;

	@Override
	public WebResult create(ConfigInfo entity) {
		WebResult ws = new WebResult();
		try {
			entity.setId(UUID.randomUUID().toString());
			configInfoMapper.createParameters(entity);
			ws.setRetcode("0");
			ws.setErrorMsg("success");
			ws.setData("数据插入成功");
		} catch (Exception e) {
			ws.setRetcode("999");
			ws.setErrorMsg("数据插入失败--->" + e.getMessage());
		}
		return ws;
	}

	@Override
	public WebResult remove(ConfigInfo entity) {
		WebResult ws = new WebResult();
		try {
			configInfoMapper.removeParameters(entity);
			ws.setRetcode("0");
			ws.setErrorMsg("success");
			ws.setData("数据删除成功");
		} catch (Exception e) {
			ws.setRetcode("999");
			ws.setErrorMsg("数据删除失败--->" + e.getMessage());
		}
		return ws;
	}

	@Override
	public WebResult modify(ConfigInfo entity) {
		WebResult ws = new WebResult();
		try {
			configInfoMapper.updateParameters(entity);
			ws.setRetcode("0");
			ws.setErrorMsg("success");
			ws.setData("数据修改成功");
		} catch (Exception e) {
			ws.setRetcode("999");
			ws.setErrorMsg("数据修改失败--->" + e.getMessage());
		}
		return ws;

	}


	@Override
	public WebResult searchAll() {
		WebResult ws = new WebResult();
		Map<String, Object> map = new HashMap<>();
		try {
			List<ConfigInfo> configInfos = configInfoMapper.readParameters();
			ws.setRetcode("0");
			ws.setErrorMsg("success");
			map.put("total", configInfos.size());
			map.put("info", configInfos);
		} catch (Exception e) {
			ws.setRetcode("999");
			ws.setErrorMsg("查询数据出错---->" + e.getMessage());
		}
		ws.setData(map);
		return ws;
	}

	@Override
	public WebResult searchByPaging(Integer currentPage, Integer size) {
		WebResult ws = new WebResult();
		Map<String, Object> map = new HashMap<>();
		try {
			PageHelper.startPage(currentPage, size);
			List<ConfigInfo> configInfos = configInfoMapper.readParameters();
			PageInfo<ConfigInfo> pageInfo = new PageInfo<>(configInfos);
			ws.setRetcode("0");
			ws.setErrorMsg("success");
			map.put("total", pageInfo.getTotal());
			map.put("info", pageInfo.getList());
		} catch (Exception e) {
			ws.setRetcode("999");
			ws.setErrorMsg("查询数据出错---->" + e.getMessage());
		}
		ws.setData(map);
		return ws;
	}

	@Override
	public WebResult searchByCondition(Integer currentPage, Integer size, ParamCondition<ConfigInfo> paramCondition) {
		WebResult ws = new WebResult();
		Map<String, Object> map = new HashMap<>();
		try {
			PageHelper.startPage(currentPage, size);
			List<ConfigInfo> configInfos = configInfoMapper.searchByCondition(currentPage, size, paramCondition);
			PageInfo<ConfigInfo> pageInfo = new PageInfo<>(configInfos);
			ws.setRetcode("0");
			ws.setErrorMsg("success");
			map.put("total", pageInfo.getTotal());
			map.put("info", pageInfo.getList());
		} catch (Exception e) {
			ws.setRetcode("999");
			ws.setErrorMsg("查询数据出错---->" + e.getMessage());
		}
		ws.setData(map);
		return ws;
	}


}
